// pages/Login/Login.js
var MD5 = require('../../utils/MD5.js');	//MD5工具类
var openid;
Page({
	data: {
		//直接进入
		username: '',
		password: '',
		login_error_hidden: true,
		error_text: ""
	},

	// 获取输入账号 
	usernameInput: function (e) {
		this.setData({
			username: e.detail.value
		})
	},

	// 获取输入密码 
	passwordInput: function (e) {
		this.setData({
			password: e.detail.value
		})
	},

	//登陆失败后的确定
	confirm: function () {
		this.setData({
			login_error_hidden: true
		});
		console.log("clicked confirm");
	},

	//确认绑定微信账户
	confirm_bund: function () {

	},
	//登录 
	login: function () {
		var _this = this;
		if (this.data.username.length == 0) {
			this.setData({
				login_error_hidden: false,
				error_text: "用户名不能为空"
			});
		} else if (this.data.password.length == 0) {
			this.setData({
				login_error_hidden: false,
				error_text: "密码不能为空"
			});
		} else {	//用户名和密码不为空
			var user_name = this.data.username;
			var pwd = this.data.password;
			pwd = MD5.MD5(pwd);
			var login_flag = true;
			//实际登陆
			wx.request({
				url: 'https://www.wosieger.com/XigeWeChat/LoginServlet',
				method: 'POST',
				header: {
					'Accept': 'application/json',
					'username': user_name,
					'password': pwd
				},
				//返回成功
				success: function (res) {
					if (res.data.code === "success") {	//成功登陆
						//存储用户输入的数据 密码要存加密前的
						wx.setStorageSync('username', user_name);
						wx.setStorageSync('password', _this.data.password);
						wx.showToast({
							title: '登录成功',
							icon: 'success',
							duration: 1000,
							success: function () {	//设置成功状态
								login_flag = true;
							}
						})
					} else {	//登陆失败
						login_flag = false;
					}
				}
			});
			if (!login_flag) {
				_this.setData({
					login_error_hidden: false,
					error_text: "登陆失败!"
				});
			} else {
				//检查用户是否绑定过微信账户
				wx.request({
					url: 'https://www.wosieger.com/XigeWeChat/QueryUserBoundServlet',
					method: 'POST',
					header: {
						'Accept': 'application/json',
						'openid': _this.data.openid
					},
					success: function (res) {
						if (res.data=== 1) {	//已经绑定
							console.log("已经绑定");
							wx.navigateTo({
								url: '../MyCourse/MyCourse?userid=' + user_name
							});
						} else {	//仍未绑定
							console.log("未绑定");
							var pwd = _this.data.password;
							pwd = MD5.MD5(pwd);
							//为该用户绑定账户
							wx.request({
								url: 'https://www.wosieger.com/XigeWeChat/UserBundFromMiniServlet',
								method: 'POST',
								header: {
									'Accept': 'application/json',
									'username': _this.data.username,
									'password': pwd,
									'openid': _this.data.openid
								},
								success: function (res) {
									if (res.code === "success") {
										wx.navigateTo({
											url: '../MyCourse/MyCourse?userid=' + user_name
										});
									} else {
										wx.navigateTo({
											url: '../MyCourse/MyCourse?userid=' + user_name
										});
									}
								}
							})
						}
					}
				});
			}
			//测试登陆
			// if (this.data.username == "123" && this.data.password == "123") {
			// 	wx.setStorageSync('username', this.data.username);
			// 	wx.setStorageSync('password', this.data.password);
			// 	var user_name=this.data.username;
			// 	wx.showToast({
			// 		title: '登录成功',
			// 		icon: 'success',
			// 		duration: 1000,
			// 		success: function () {//接口调用成功的回调函数  
			// 			wx.navigateTo({
			// 				url: '../MyCourse/MyCourse?userid='+user_name
			// 			})
			// 		}
			// 	})
			// } else {
			// 	this.setData({
			// 		login_error_hidden: false,
			// 		error_text: "登陆失败!"
			// 	});
			// }
		}
	},

	//加载完后，处理事件 
	// 如果有本地数据，则直接显示
	onLoad: function (options) {
		var _this = this;
		wx.login({
			success: function (loginCode) {
				var appid = 'wxd984fbbb4ffcaa1b'; //填写微信小程序appid  
				var secret = '467bdc1dc4fe509f425f841e1327b218'; //填写微信小程序secret  
				//调用request请求api转换登录凭证  
				wx.request({
					url: 'https://api.weixin.qq.com/sns/jscode2session?appid=' + appid + '&secret=' + secret + '&grant_type=authorization_code&js_code=' + loginCode.code,
					header: {
						'content-type': 'application/json'
					},
					success: function (res) {
						console.log(res.data.openid) //获取openid  
						_this.setData({
							openid: res.data.openid
						});
					}
				})
			}
		});

		//获取本地数据
		var username = wx.getStorageSync('username');
		var password = wx.getStorageSync('password');
		console.log(username);
		console.log(password);
		if (username) {
			this.setData({ username: username });
			// document.getElementById("username_input").innerHTML =username;
		}
		if (password) {
			this.setData({ password: password });
			// document.getElementById("password_input").innerHTML = password;
		}
	},
})